////////////////////////////////////////////////////////////////
// Sunil Suram (PhD Candidate)								  //
// Prof. Mark Bryden                                          //
// Virtual Engineering Research Group                         //
// Iowa State University, Ames, IA, 50010                     //
////////////////////////////////////////////////////////////////

#include "pod.h"
#include <gsl/gsl_vector.h>
#include <cmath>
#ifdef WIN32
#include <windows.h>
#endif

#include "podversdef.h"
#define hypot _hypot
void print_usage();
void print_version();

/////////////////////////////////////////////////////////////////
int main (int argc, char *argv[])
{

	int num_dims;  // number of dimensions
    pod *p = NULL;        // the pod object

	// do we have at least 3 args?
	if(argc<3)
	{
		if ( argc==1)
		{	
			print_usage();
			exit(0);
		}
		else if( (strcmp(argv[1],"-v")==0))
		{
			print_version();
			exit(0);
		}
	}

	// rom evaluation mode
	if(strcmp(argv[1],"-eval")==0)
	{
		int numModes = 0;
		num_dims = atoi(argv[2]);

		if(argc < (3+num_dims))
		{
			print_usage();
			exit(0);
		}

		// evaluate using existing ROM
		p = new pod( );
		p->SetMode(string("-eval"));

		vector<double> val;
		for(int i=0;i<num_dims;i++)
		{
			val.push_back(atof(argv[3+i]));
		}
		if ( argc == (4+num_dims) )
			numModes = atoi( argv[3+num_dims] );

		// do eval using ROM
		p->ReadROM();
		//p->ReadDataFromFile(".\\Results\\info.txt");
		//p->SetInterpolationType( 1 );
		p->ComputePODApproximation( val, numModes );

		//write out energetic modes
		for ( int i=0;i<p->GetEnergeticModes();i++ )
			p->WriteMode( i );


	}
	else if(strcmp(argv[1],"-construct")==0)  // rom construction mode
	{
		if(argc!=3)
		{
			print_usage();
			exit(0);
		}

		// construct a ROM using datasets
		string inpFile = string(argv[2]);
		//make a directory to write the results
#ifdef WIN32
		DWORD status;
		CreateDirectory( L"./Results", NULL);
		status = GetLastError();
		if ( status==ERROR_PATH_NOT_FOUND )
			exit( ERROR_PATH_NOT_FOUND );
#else
		system( "mkdir -p ./Results" );
#endif

		p = new pod( );
		p->SetMode(string("-construct"));
		p->ReadDataFromFile( inpFile.c_str( ) );
		p->ComputeCoefficients( );
		p->ComputeEnergy( 0.0001 );  //capture 99.99% of the energy
		p->WriteInfo();
		
	}
	else
	{
		print_usage();
		exit(0);
	}

	//clean up
	if(p)delete p;
	return(0);
}

/////////////////////////////////////////////////////////////////
void print_usage()
{
	cout<<"Usage: "<<endl;
	cout<<"Evaluation Mode Command Line  : -eval <num_dimensions> <eval_pt_dim1> <eval_pt_dim2> ...<eval_pt_dimn> [num modes]"<<endl;
	cout<<"Construction Mode Command Line: -construct <dataset file> -model <modelname>"<<endl;
	cout<<"Dump Designs Mode Command Line: -dd"<<endl;
}


void print_version()
{
	cout<<podMajorVers<<"."<<podMinorVers<<endl;
}